package cvut.fit.dpo.dfs;

import java.util.LinkedList;
import java.util.Queue;

import cvut.fit.dpo.arithmetic.ArithmeticExpression;
import cvut.fit.dpo.arithmetic.IComponent;
import cvut.fit.dpo.arithmetic.NumericOperand;
import cvut.fit.dpo.arithmetic.parsing.AddOperation;
import cvut.fit.dpo.arithmetic.parsing.CloseBracketOperation;
import cvut.fit.dpo.arithmetic.parsing.ExpressionElement;
import cvut.fit.dpo.arithmetic.parsing.Number;
import cvut.fit.dpo.arithmetic.parsing.OpenBracketOperation;
import cvut.fit.dpo.arithmetic.parsing.SubstractOperation;

public class InOrderStrategy implements IStrategy {

	private Queue<ExpressionElement> queue;
	
	public InOrderStrategy(){
		this.queue = new LinkedList<ExpressionElement>();
	}
	
	@Override
	public Queue<ExpressionElement> Search(IComponent component) {
		
		if(component instanceof ArithmeticExpression && ((ArithmeticExpression)component).getOperator() != null){
			queue.add(new OpenBracketOperation());
			Search(((ArithmeticExpression)component).getOperator().getFirstOperand());
		}
		if(component instanceof NumericOperand){
			queue.add(new Number(component.evaluate()));
		}
		else if(component instanceof ArithmeticExpression && ((ArithmeticExpression)component).getOperator() != null){
			if(component.toString().contains("+")) queue.add(new AddOperation());
			if(component.toString().contains("-")) queue.add(new SubstractOperation());
			//if(component.toString().contains(")")) stack.push(new CloseBracketOperation());
			//if(component.toString().contains("(")) stack.push(new OpenBracketOperation());
		}
		else if(component instanceof ArithmeticExpression && ((ArithmeticExpression)component).getOperator() == null){
			queue.add(new Number(((ArithmeticExpression)component).getValue()));
		}
		if(component instanceof ArithmeticExpression && ((ArithmeticExpression)component).getOperator() != null){
			
			Search(((ArithmeticExpression)component).getOperator().getSecondOperand());
			queue.add(new CloseBracketOperation());
		}
		
		return this.queue;
	}
	
}
